<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>价目表管理</title>
<%@include file="../include/include.jsp"%>
<link rel="stylesheet" type="text/css" href="${csspath }/public.css" />
<script type="text/javascript" src="${jspath}/jquery/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css"
	href="${bootstrapPath}/css/bootstrap.css" />
<script type="text/javascript" src="${bootstrapPath}/js/bootstrap.js"></script>
<script type="text/javascript"
	src="${bootstrapPath}/js/bootstrap3-validation.js"></script>
<script type="text/javascript" src="${layerPath}/layer.js"></script>
<script type="text/javascript" src="${jspath}/jquery/jquery.alerts.js"></script>
</head>

<script type="text/javascript">
	var row_count = 0;
	$(function() {

		$("#returnBtn").click(
				function() {
					$("#ticketLinePriceForm").attr('action',
							'${webcontext}/crmdp/dealer/coupon/toEditTicket');
					$("#ticketLinePriceForm").submit();
				});
		$("#saveBtn")
				.click(
						function() {
							var priceStr = "";
							if (row_count >= 1) {
								for ( var k = 0; k < row_count; k++) {
									var code = $("#code" + k).val();
									var value = $("#value" + k).val();
									var price = $("#price" + k).val();
									if (code != "N/A" && code != undefined
											&& code != null && code != "") {
										priceStr = priceStr + code + "@#"
												+ value + "@#" + price + "###";
									}
								}
							}
							if (priceStr == "") {
								jAlert("没有新增任何数据", "无法提交");
								return false;
							}
							$
									.ajax({
										url : '${webcontext}/crmdp/dealer/coupon/saveTicketLinePriceinfo',
										type : 'post',
										data : {
											'ticketLineId' : $("#ticketLineId")
													.val(),
											'codeType' : $("#codeType").val(),
											'priceStr' : priceStr
										},
										success : function(data) {
											if (data.code == true) {
												jAlert(
														data.msg,
														"提示",
														function() {
															$(
																	"#ticketLinePriceForm")
																	.attr(
																			'action',
																			'${webcontext}/crmdp/dealer/coupon/toTicketLinePricePage');
															$(
																	"#ticketLinePriceForm")
																	.submit();
														});
											} else if (data.code == false) {
												jAlert(data.msg, "提示");
											}
										},
										error : function() {
											jAlert("服务器错误，请联系管理员", "提示");
										}
									});
						});
	});

	function insertRow() {
		var table = $("#data_tabs");
		var row = $("<tr></tr>");
		var td0 = $("<td></td>");
		var td1 = $("<td></td>");
		var td2 = $("<td></td>");
		var td3 = $("<td></td>");
		td0
				.append($("<select id='code"
						+ row_count
						+ "' class='form-control input-sm' style='width: 220px;' <option value='N/A'>--------请选择门店属性---------</option><core:forEach items='${codeInfoList}' var='list'><option value='${list.code}'>${list.value}</option></core:forEach></select>"));
		row.append(td0);

		td1
				.append($("<input class='form-control input-sm text-left' id='value"+row_count+"' type='text' />"));
		row.append(td1);

		td2
				.append($("<input class='form-control input-sm text-right' id='price"+row_count+"' type='text'/>"));
		row.append(td2);

		td3
				.append($("<button type='button' id='delete' class='btn btn-warning btn-sm' onclick='deleteRow(this.id)' style='margin-left: 10px;'>删除</button> "));
		row.append(td3);

		table.append(row);
		row_count++;
	}

	//删除
	function removeMealPrice(id) {
		//询问框
		jConfirm(
				"确定要删除数据吗？",
				"确认",
				function(r) {
					if (r) {
						$
								.ajax({
									url : '${webcontext}/crmdp/dealer/coupon/removeTicketLinePrice',
									type : 'post',
									data : {
										'id' : id
									},
									success : function(data) {
										if (data.code == true) {
											jAlert(
													data.msg,
													"提示",
													function() {
														$(
																"#ticketLinePriceForm")
																.attr('action',
																		'${webcontext}/crmdp/dealer/coupon/toTicketLinePricePage');
														$(
																"#ticketLinePriceForm")
																.submit();
													});
										} else if (data.code == false) {
											jAlert(data.msg, "提示");
										}
									},
									error : function() {
										jAlert("服务器错误，请联系管理员", "提示");
									},
								});
					}
				});
	}

	//删除行
	function deleteRow(id) {
		$("#" + id).parent().parent().remove();
	}

	//修改价目表的详细信息
	function updateMealPrice(id) {
		var ticketLineId = $("#ticketLineId").val();
		var ticketDefId = $("#ticketDefId").val();
		var qticketType = $("#qticketType").val();
		var qticketName = $("#qticketName").val();
		var pager = $("#pager").val();
		layer
				.open({
					type : 2,
					title : '门店修改',
					shadeClose : false,
					closeBtn : false,
					shade : 0.1,
					offset : [ '16%', '28%' ],
					area : [ '560px', '300px' ],
					content : '${webcontext}/crmdp/dealer/coupon/toEditTicketLinePricePage?id='
							+ id
							+ "&ticketLineId="
							+ ticketLineId
							+ "&ticketDefId="
							+ ticketDefId
							+ "&qticketType="
							+ qticketType
							+ "&qticketName="
							+ qticketName
							+ "&pager.currentPage=" + pager
				});
	}
</script>
<style>
.hr {
	text-align: center;
	width: 100%;
	background-color: #0071C1;
	height: 1px;
	margin-bottom: 10px;
}
</style>
<body>
	<div id="content">
		<div style="color: #025D7B; height: 26px; line-height: 26px;">
			<span>套餐修改</span> <span>&gt;</span> <span>价目表管理</span>
		</div>

		<div class="hr"></div>
		<form:form id="ticketLinePriceForm" action="" class="form-horizontal"
			method="post" modelAttribute="dto" role="form">
			<input type="text" class="hidden" id="ticketDefId" name="ticketDefId"
				value="${ticketDefId}" />
			<input type="text" class="hidden" id="ticketLineId"
				name="ticketLineId" value="${ticketLineId}" />
			<input type="text" class="hidden" id="codeType" name="codeType"
				value="${codeType}" />

			<input type="text" class="hidden" id="qticketType" name="qticketType"
				value="${qticketType}" />
			<input type="text" class="hidden" id="qticketName" name="qticketName"
				value="${qticketName}" />
			<input type="text" class="hidden" id="pager" name="pager.currentPage"
				value="${currentPage}">

			<table id="data_tabs" class="table"
				style="margin-top: 20px; margin-left: 20px; width: 98%;">
				<thead>
					<tr>
						<th class="hidden">ID</th>
						<th class="hidden">套餐ID</th>
						<th>门店属性</th>
						<th>属性值</th>
						<th>价格</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<core:forEach items="${mealPriceList}" var="list">
						<tr>
							<td class="hidden">${list.id}</td>
							<td class="hidden">${list.ticketLineId}</td>
							<td>${list.code}</td>
							<td>${list.value}</td>
							<td class="text-right">${list.price}</td>
							<td class="text-left">
								<button type="button" class="btn btn-success btn-xs"
									onClick="updateMealPrice('${list.id}')">
									<i class="glyphicon glyphicon-pencil" style="color: #FFFFFF"></i>&nbsp;修改
								</button>
								<button type="button" class="btn btn-success btn-xs"
									onClick="removeMealPrice('${list.id}')">
									<i class="glyphicon glyphicon-trash" style="color: #FFFFFF"></i>&nbsp;删除
								</button>
							</td>
						</tr>
					</core:forEach>
				</tbody>
			</table>
			<div style="margin-left: 300px;">
				<button type="button" class="btn btn-success btn-sm" id="saveBtn">
					<i class="glyphicon glyphicon-floppy-saved" style="color: #FFFFFF"></i>&nbsp;保
					存
				</button>
				<button type="button" class="btn btn-success btn-sm"
					style="width: 100px;" onclick="insertRow()">
					<i class="glyphicon glyphicon-plus" style="color: #FFFFFF;"></i>&nbsp;价目表新增
				</button>
				<button type="button" class="btn btn-success btn-sm" id="returnBtn">
					<i class="glyphicon glyphicon-retweet" style="color: #FFFFFF"></i>&nbsp;返回
				</button>
				<span id="msgInfo" class="text-danger"></span>
			</div>
		</form:form>
	</div>
</body>
</html>